package com.sl.plugin.step.field;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import com.sl.core.engine.meta.impl.AbstractProcessCompDescriptor;
import com.sl.core.engine.step.ProcessCompService;
import com.sl.core.engine.step.impl.AbstractProcessComp;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 */
//@Compensation(listener = NormalRowListener.class, type = CompensationType.SERVICE_COMPENSATION)
public class FieldNameConvertComp extends AbstractProcessComp<Map<String, Object>, Map<String, Object>> implements ProcessCompService {

    public FieldNameConvertComp(AbstractProcessCompDescriptor abstractProcessCompDescriptor) {
        super(abstractProcessCompDescriptor);
    }


    @Override
    public Map<String, Object> input(Map<String, Object> param) {

        Map<String, Object> am = meta.getAttributeMap();

        Map<String, Object> output = MapUtil.get(am, "config", Map.class);

        String upperTrans = MapUtil.getStr(output, "upperTrans");
        String underLineTrans = MapUtil.getStr(output, "underLineTrans");

        //大写
        if (StrUtil.equals(upperTrans, "upper")) {
            toLowerCaseUnderCame(param, true, false, false, false);
            //小写
        } else if (StrUtil.equals(upperTrans, "lower")) {
            toLowerCaseUnderCame(param, false, true, false, false);
        }

        //大写
        if (StrUtil.equals(underLineTrans, "underLine")) {
            toLowerCaseUnderCame(param, false, false, true, false);
        } else if (StrUtil.equals(underLineTrans, "cameCase")) {
            toLowerCaseUnderCame(param, false, false, false, true);
        }
        return param;
    }

    private void toLowerCaseUnderCame(Map<String, Object> map, Boolean upper, Boolean lower, Boolean underLine, Boolean cameCase) {
        if (MapUtil.isEmpty(map)) {
            return;
        }
        List<String> strings = map.keySet().stream().toList();
        for (String name : strings) {
            Object value = map.get(name);
            map.remove(name);

            if (BooleanUtil.isTrue(upper)) {
                map.put(name.toUpperCase(), value);
            }
            if (BooleanUtil.isTrue(lower)) {
                map.put(name.toLowerCase(), value);
            }
            if (BooleanUtil.isTrue(underLine)) {
                map.put(StrUtil.toUnderlineCase(name), value);
            }
            if (BooleanUtil.isTrue(cameCase)) {
                map.put(StrUtil.toCamelCase(name), value);
            }
            if (value instanceof Collection valueMetaArray) {
                for (Object o : valueMetaArray) {
                    if (o instanceof Map m) {
                        toLowerCaseUnderCame(m, upper, lower, underLine, cameCase);
                    }
                }
            } else if (value instanceof Map valueMetaObject) {
                toLowerCaseUnderCame(valueMetaObject, upper, lower, underLine, cameCase);
            }
        }


    }


}
